package at.hockey.ticker;

import at.hockey.ticker.R;

import android.content.Context;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class MatchOverviewEntryUI extends LinearLayout implements OnTouchListener {
	
	private GestureDetector mGestureDetector; 
	private HockeyTickerUI mHTUI;
	private ViewFlipper mVF;
	private MatchDetails mMD;
	private TextView mTV1, mTV2, mTV3, mTV4, mTV5;
	
	public MatchOverviewEntryUI(HockeyTickerUI root, MatchDetails md) {
		super(root);

		this.mHTUI = root;
		
		//instantiate xml layout
		LayoutInflater inflater = (LayoutInflater) this.mHTUI.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.matchoverviewentry, this);

        this.mGestureDetector = new GestureDetector(new OnFlingGestureListener(this.mHTUI));
		
		this.mVF = (ViewFlipper) this.mHTUI.findViewById(R.id.main_vf);
		this.mMD = md;

		this.mTV1 = (TextView) this.findViewById(R.id.moe_team1_view);
		this.mTV1.setText(this.mMD.getTeam1());

		this.mTV2 = (TextView) this.findViewById(R.id.moe_score_view);
		this.mTV2.setText(this.mMD.getScore());

		this.mTV3 = (TextView) this.findViewById(R.id.moe_team2_view);
		this.mTV3.setText(this.mMD.getTeam2());
		
		this.mTV3 = (TextView) this.findViewById(R.id.moe_text1_view);
		this.mTV3.setText(this.mMD.getText1());

		this.mTV4 = (TextView) this.findViewById(R.id.moe_text2_view);
		this.mTV4.setText(this.mMD.getText2());

		this.mTV5 = (TextView) this.findViewById(R.id.moe_text3_view);
		this.mTV5.setText(this.mMD.getText3());

		this.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent e) {

		if(mGestureDetector.onTouchEvent(e)){
			return true;
		}
		else{
			float eventX = v.getLeft() + e.getX();
			float eventY = v.getTop() + e.getY();
			
			if(e.getAction() == MotionEvent.ACTION_UP && eventX >= v.getLeft() && eventX <= v.getRight() && eventY >= v.getTop() && eventY <= v.getBottom()){

				for(int i=1; i<this.mVF.getChildCount(); i++){

					MatchDetailsUI mdui = (MatchDetailsUI) this.mVF.getChildAt(i); 
					if(mdui.getMatchDetails().equals(this.mMD)){
					
						if(i == 1){
							
							Animation inFromRight = AnimationUtils.loadAnimation(this.mHTUI, R.anim.anim_in_from_right);
							this.mVF.setInAnimation(inFromRight);

							Animation outToLeft = AnimationUtils.loadAnimation(this.mHTUI, R.anim.anim_out_to_left);
							this.mVF.setOutAnimation(outToLeft);
						}
						else if(i == this.mVF.getChildCount()-1){
							
							Animation inFromLeft = AnimationUtils.loadAnimation(this.mHTUI, R.anim.anim_in_from_left);
							this.mVF.setInAnimation(inFromLeft);

					    	Animation outToRight = AnimationUtils.loadAnimation(this.mHTUI, R.anim.anim_out_to_right);
					    	this.mVF.setOutAnimation(outToRight);
						}
						else{
							
							Animation inFromCenter = AnimationUtils.loadAnimation(this.mHTUI, R.anim.anim_in_from_center);
							this.mVF.setInAnimation(inFromCenter);

							Animation outToCenter = AnimationUtils.loadAnimation(this.mHTUI, R.anim.anim_out_to_center);
							this.mVF.setOutAnimation(outToCenter);
						}
						
						this.mVF.setDisplayedChild(i);
						this.mHTUI.setAppTitle();
					}
				}
			}
			return true;
		}
	}

}
